﻿using System;
using System.Collections.Generic;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Cms.Domain;
using Cms.Web.Framework.Controls;

namespace Cms.Web.UI.Admin.HelperClasses
{
    public class ContentSetContainerHelper
    {
        public static void SetVersions(
            IList<ContentTranslationVersion> versions,
            int publishedVersionID,
            string noContentVersionTranslation,
            ref DropDownList ddlVersions,
            ref LinkButton lbPublish,
            ref HtmlAnchor hlPublishLater,
            ref LinkButton lbSelectPublishedVersion,
            ref LocalizedLiteral llNoVersionPublished)
        {
            ddlVersions.Items.Clear();
            if (versions.Count == 0)
            {
                ddlVersions.Items.Add(new ListItem(noContentVersionTranslation, "-1"));
                lbPublish.Visible = false;
                hlPublishLater.Visible = false;
            }
            else
            {
                foreach (ContentTranslationVersion t in versions)
                {
                    string shortDate = new DateTime(long.Parse(t.Timestamp)).ToShortDateString();
                    string shortTime = new DateTime(long.Parse(t.Timestamp)).ToShortTimeString();
                    ddlVersions.Items.Add(new ListItem(shortDate + " " + shortTime + " #" + t.ID, t.ID.ToString()));
                }
                lbPublish.Visible = true;
                hlPublishLater.Visible = true;
            }
            if (publishedVersionID > 0)
            {
                // There might be a better way of doing this... :-)
                // Especially since ddlVersions always should show the latest available version.
                ddlVersions.SelectedValue = publishedVersionID.ToString();
                lbSelectPublishedVersion.Text = ddlVersions.SelectedItem.Text;

                lbSelectPublishedVersion.CommandArgument = publishedVersionID.ToString();
                llNoVersionPublished.Visible = false;
                lbSelectPublishedVersion.Visible = true;
            }
            else
            {
                llNoVersionPublished.Visible = true;
                lbSelectPublishedVersion.Visible = false;
            }
            ddlVersions.SelectedIndex = 0;
        }
    }
}